
Player=class("Player");

function Player:ctor(obj)
    self.gameObject=obj;
    self.transform=obj.transform;

    self.nameText=self.transform:Find("name"):GetComponent('UILabel');
    self.socreText=self.transform:Find("score"):GetComponent('UILabel');
    self.readyText=self.transform:Find("ready"):GetComponent('UILabel');--状态：是否准备
    self.landlord=self.transform:Find("landlord").gameObject;--是否是地主

    --存储的牌
    self.handPais={}
    self.outPais={}

    self.handContainer = self.transform:Find("handContainer");
    self.outContainer = self.transform:Find("outContainer");

end

function Player:Init(user)
    self.nameText.text = user.username;
    self.socreText.text = user.score;
    self.readyText.text = user.isReady and "准备" or "";
    self.landlord:SetActive(false);
end

--设置准备
function Player:ShowReady(isReady)
    self.readyText.text = "准备";
end

--显示叫地主
function Player:ShowRobTurn()
    self.readyText.text = "抢地主中";
end

--显示出牌阶段
function Player:ShowPutTurn()
    self.readyText.text = "出牌中";
    self:ClearOutView();
end

--展示不叫地主
function Player:ShowRobTurn()
    self.readyText.text = "不叫";
end

--展示要不起
function Player:ShowNotPut()
    self.readyText.text = "要不起";
end

--设置状态
function Player:SetState(message)
    message = message or "";
    self.readyText.text = message;
end

--显示地主
function Player:ShowLandLord(isOn)
    
    self.landlord:SetActive(isOn);
    if (isOn) then
        self:ShowPutTurn();
    end
end

--设置手牌
function Player:SetPais(pais)
    local count = #pais/2;
    -- i 代表第几张 paiId 代表什么牌
    for i,paiId in ipairs(pais) do
        local card = ResourcesFactory.CreateCard(paiId);   
        card.transform:SetParent(self.handContainer,false);

        --缓存到表中
        table.insert(self.handPais,card);
    end
  
    self:SortPais(self.handPais);
end

--给牌排序
function Player:SortPais(pais)
    table.sort(pais,
    function(a,b)
        if(a.num == b.num)then
            return a.color >b.color;
        else
            return a.num >b.num;
        end      
    end);

    --重新给牌赋位置
    local count = #pais/2;
    for i,card in ipairs(pais) do
        card.transform.localPosition = Vector3((i-count)*60,0,0)
        card.panel.depth =i;
    end
end

--玩家出牌
function Player:PutOut()
    local req = {seatId = GamePanel.roomdata.seatId,pais={}}

    for i,card in ipairs(self.handPais) do
        if(card.shoot)then
            table.insert(req.pais,card.paiId);
        end
    end
    --如果点起的牌的数量不等于0
    if(#req.pais ~= 0)then
        log("出牌")
        --发送到服务器 出牌的消息
        local message = protobuf.encode("lin.ReqPais",req);
        Network.Send(Protocal.ReqPutCard,message);
    end
end

--提示
function Player:Tips()

end

--要不起
function Player:Pass()

end



--展示出牌成功
function Player:ShowPutCard(pais)
    --清空出牌区
    self:ClearOutView();

    --展示出牌区
    for i,paiId in ipairs(pais) do
        --移除手牌区
        local index,card = self:GetCard(paiId);

        if (index~=nil) then
            table.remove(self.handPais,index);
        end
        --防止非本机玩家手牌区没有牌
        if (card==nil) then
            card = ResourcesFactory.CreateCard(paiId)
        end
        --添加到出牌区
        card.transform:SetParent(self.outContainer,false);
        table.insert( self.outPais,card);               
    end

    --出牌后重新排序
    self:SortPais(self.handPais);

    --重新排序出牌区
    self:SortPais(self.outPais);
end

--清空出牌区
function Player:ClearOutView()
    for i,card in ipairs(self.outPais) do
        destroy(card.gameObject);
    end
    self.outPais = {};
end

--清空手牌区
function Player:ClearHandView()
    print("Player:ClearHandView()");
    for i,card in ipairs(self.handPais) do
        destroy(card.gameObject);
    end
    self.handPais = {};
end

--清空上局数据
function Player:Clear()
    self:ClearOutView();
    self:ClearHandView();
    self:ShowLandLord(false);
end

function Player:GetCard(paiId )
    for i,card in ipairs(self.handPais) do
        if(card.paiId==paiId)then
            return i,card ;
        end
    end
    return nil;
end